import { Provide } from '@midwayjs/decorator';
import { UserEntity } from '../entity/user';
import { IUserOptions } from '../interface';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository    } from 'typeorm'
import  axios from  'axios'
// var tough = require("tough-cookie");

// import  cheerio from  'cheerio'
@Provide()   // 只要是 Service 必须添加 , 想要识别到这个class
export class TestService {

    @InjectEntityModel(UserEntity)
    UserModel: Repository<UserEntity>

  async getUser(options: IUserOptions) {
    return {
      uid: options.uid,
      username: 'mockedName',
      phone: '12345678901',
      email: 'xxx.xxx@xxx.com',
    };
  }
  async getdata() {
  let {data } =await  axios({
        url:"https://www.yuque.com/api/books/32556122/overview?",
        headers:{
          // cookie:'_yuque_session=5ZBHoRQuNa9Cir_GW-U-6SYQKBuJ7cl2EVgCeijnVKPK4IEXVkDWAiw_jEf_sv24yrHIIEVqCzIyqFYyzwPjNA==;lang=zh-cn;  acw_tc=0bca30fe16710178557643644e5e777c781bbcc265e3a1b50492020af873c0; yuque_ctoken=kgw6xTJJAZHL1rRbL1hDx19g; current_theme=default'
          cookie:"lang=zh-cn; _yuque_session=5ZBHoRQuNa9Cir_GW-U-6SYQKBuJ7cl2EVgCeijnVKPK4IEXVkDWAiw_jEf_sv24yrHIIEVqCzIyqFYyzwPjNA==; yuque_ctoken=kgw6xTJJAZHL1rRbL1hDx19g; current_theme=default; acw_tc=0bca30fe16710238991721727e5e6923a3e82ef3e24d15ce926d70d7960d6f"
        }
    })
    console.log("语雀数据",data);
    
    return   1
  }
  async cheerio2() {
    let html =await  axios({
      url:"https://www.yuque.com/api/accounts/login",

  })
  //   let html =await  axios({
  //     url:"https://www.yuque.com/api/accounts/login",
  //     method:'post',
  //     data:{
  //       login:'15917902838',
  //       loginType:"password",
  //       password:"QQvqkHvdKlKRE4WLy4Ze8bL6+kdW58SXAIpofW+bRtoGV5h6teo3kFJ+CIrWfC7o2iyALzXqXfhNxtr6AGAmMpmaiRv2XESBEl1yKAoui7+B/yHr0okf+4i/W4WpZkRXjU5VysDyprJCx2jLzIFYkLNKiYWz9bNzb+T1+0ROH50="

  //     },
  //     headers:{
  //       Accept: 'application/json',
  //       'Content-Type':'application/json',
  //       "Referer":'https://www.yuque.com/login?goto=https%3A%2F%2Fwww.yuque.com%2Fg%2Fyuqueyonghunowms0%2Fgxey6g%2Fcollaborator%2Fjoin%3Ftoken%3DlwL892js4HpwTA9T',
  //       "sec-ch-ua":'"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
  //       "sec-ch-ua-mobile":"?0",
  //       "sec-ch-ua-platform":"Windows",
  //       "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
  //       "x-csrf-token":"Ib-KEu26ZdQe0XW6O2-MrfnF",
  //       "X-Requested-With":"XMLHttpRequest"
        
  //     }
  // })
    console.log(html);
    
// console.log( html);
    
  // let cook =  Cookie.parse( html.headers['set-cookie'])

  // console.log("cook",cook);
  // var cookiejar = new tough.CookieJar();
  // var cookies = await cookiejar.getCookies("https://www.yuque.com/g/yuqueyonghunowms0/gxey6g/collaborator/join?token=lwL892js4HpwTA9T#");

  // var Cookie = tough.Cookie;
//  var  cookies = html.headers['set-cookie'].map(Cookie.parse);
  // console.log('cookies',cookies);
  // var datarr = html.headers['set-cookie'][3].substring(0,html.headers['set-cookie'][3].indexOf(" ") )
  // var str =datarr.join(" ")
  // datarr
  // console.log(datarr);
  let {data } =await  axios({
    url:"https://www.yuque.com/api/books/32556122/overview?",
    headers:{
      // cookie:"_yuque_session=5ZBHoRQuNa9Cir_GW-U-6SYQKBuJ7cl2EVgCeijnVKPK4IEXVkDWAiw_jEf_sv24yrHIIEVqCzIyqFYyzwPjNA==; yuque_ctoken=kgw6xTJJAZHL1rRbL1hDx19g; current_theme=default; acw_tc=0bca30fe16710238991721727e5e6923a3e82ef3e24d15ce926d70d7960d6f; lang=zh-cn; "
      // cookie:datarr
      /*acw_tc=0bca31ca16710245522895104efdd905f9d83e8191992864eaf6b916de5455; yuque_ctoken=cb7tm6jW6SqDYBZzKJtK3pB_; lang=zh-cn; _yuque_session=nkulcKtzZ1RBX6UkAEvyxmdSgqOGccG0_e9br50tgKej9bzA6RdJu9Vcgw5l-eUOdG2AwOY_OFOVHv1JRg3nuw==; current_theme=default; */
      // cookie:"_yuque_session=5ZBHoRQuNa9Cir_GW-U-6SYQKBuJ7cl2EVgCeijnVKPK4IEXVkDWAiw_jEf_sv24yrHIIEVqCzIyqFYyzwPjNA==;"
      // cookie:"_yuque_session=5ZBHoRQuNa9Cir_GW-U-6SYQKBuJ7cl2EVgCeijnVKPK4IEXVkDWAiw_jEf_sv24yrHIIEVqCzIyqFYyzwPjNA==;"
      cookie:"_yuque_session=5ZBHoRQuNa9Cir_GW-U-6SYQKBuJ7cl2EVgCeijnVKPK4IEXVkDWAiw_jEf_sv24yrHIIEVqCzIyqFYyzwPjNA=="
    }
})  
// console.log( "获取得到的数据",data);


    
    return data
  }



/* 
  前端 :
    keyword :"奥术大师多"   ? =>  模糊查询哪一列? 需要配置
    
    范围: 
    条件筛选  某某时间内  某某价格  
     where : {  Creattime:[ 开始 ,结束  ] , price:[ 1 , 500 ]   , num:[1]  }

    排序
     orderby : {  Creattime :desc , price:desc     }
     精准查询
     andwhre: [  status: [1,2,38,9]  , orderstatus:[1,2,5,3]] 
     page: 1
    size:10


    后端配置:
     配置 查询哪个表  动态变化
    支持 模糊查询 查询哪一个?   author  ,title
                            支持那些什么条件 
    查询文章表中的第38章        联查什么表 

    查询订单表中是 书籍id是 38的表

*/


  // async  getsqldata(  body , option ){

  //   // var  body  = {
  //   //      keyword:"zinan",
  //   //      orderby:{ "createDate":'desc' , "id":"ASC" },
  //   //      where : {    },
  //   //      andWhere:{}
  //   // }

  //   var  option:{
  //       keyword:['id',"name"],
  //       entity:_this.UserModel,
  //       alias:"a",
  //       // leftJoinAndSelect("a.photos", "photo")
  //   }

  //   // this.UserModel.createQueryBuilder('a').orWhere( `a.name like  :name ` ,{  name: body.keyword  }).orWhere()
  //   /*  考虑的要动态添加 orwhere */
  //   var find = option.entity.createQueryBuilder('a')

  //   if(option.leftJoinAndSelect){
  //       find.leftJoinAndSelect("a.photos", "photo")
  //   }







  //   //  keyword
  //   for( let  key in option.keyword){
  //       find.orWhere( `a.${key} like  :name ` ,{  name: body.keyword  })
  //   }

  //   //  范围 
    
  //   for( let key   in  body.where){
  //           if(  body.where[key].length ==2 ){
  //                   find.andWhere(` a.${key}   >  ${ body.where[key][0]}  `    )
  //                   find.andWhere(` a.${key}   < ${ body.where[key][1]}  `    )
  //           }else{
  //               find.andWhere(` a.${key}   =  ${ body.where[key][0]}  `)
  //           }
  //   }


  //   //  之前的or  都是属于 全局的or  
  //   //  在某某条件中 的范围出现 or

  //   //  &&   &&   &&    &&(  ||    ||  ||)

  //   find.andWhere( new Brackets(qb => {

  //       for(  let key  in  body.andWhere){
  //           qb.orWhere(` a.${key}  = ${body.andWhere[key]}  ` )
  //       }          
  //   }))


  //   // for( let key  in body.andWhere){
  //   //     find.where(  `a.id =  6`  ).orWhere
  //   // }   



    
  //   var orderobj = { }
  //   for( let key in body.orderby  ){
  //    orderobj[`a.${key}`]  =body.orderby[key]
  //   }  

  //   find.orderBy(orderobj )





  // }





}
